﻿CREATE FUNCTION [dbo].[fn_GetOpeningQuoteWeek] 
(
	@Year smallint,
	@Week smallint,
	@TickerSymbol varchar(25)
)
RETURNS smallmoney
AS
BEGIN
	
	RETURN 
	(
		SELECT
			TOP 1     
			Q.StartValue  
		FROM         
			dbo.Quote AS Q 
				INNER JOIN
            dbo.Security AS S ON Q.SecurityID = S.SecurityID 
				INNER JOIN
			dbo.Exchange AS E ON S.ExchangeID = E.EchangeID 
				INNER JOIN
			dbo.DateTime AS D ON Q.DateTimeID = D.DateTimeID
		WHERE
			D.Year=@Year 
				AND
			D.WeekNr = @Week
				AND
			S.TickerSymbol = @TickerSymbol			
		ORDER BY 
			D.DateTime	
	)
	

END